package pers.lan.stack;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/3 12:51 PM
 */
public class LC_103 {

    static class Solution {
        public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
            List<List<Integer>> res = new LinkedList<>();
            if (root == null) {
                return res;
            }
            Stack<TreeNode> stack = new Stack<>();
            Stack<TreeNode> stack1 = new Stack<>();
            stack.push(root);
            while (!stack1.empty() || !stack.empty()) {
                List<Integer> list = new LinkedList<>();
                while (!stack.empty()) {
                    TreeNode node = stack.pop();
                    list.add(node.val);
                    if (node.left != null) {
                        stack1.push(node.left);
                    }
                    if (node.right != null) {
                        stack1.push(node.right);
                    }
                }
                if (!list.isEmpty()) {
                    res.add(list);
                }
                list = new LinkedList<>();
                while (!stack1.empty()) {
                    TreeNode node = stack1.pop();
                    list.add(node.val);
                    if (node.right != null) {
                        stack.push(node.right);
                    }
                    if (node.left != null) {
                        stack.push(node.left);
                    }
                }
                if (!list.isEmpty()) {
                    res.add(list);
                }

            }


            return res;
        }
    }

    static class TreeNode {

        int val;

        TreeNode left;

        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

}
